import ElementUI from 'element-ui'
import axios from 'axios';

var ajax = axios.create({
    baseURL: process.env.BASE_URL,
    headers: {},
    withCredentials: true, //cookie
    crossDomain: true //跨域
});

//添加一个请求拦截器
ajax.interceptors.request.use(function(config) {
    //在请求发出之前进行一些操作
    if (sessionStorage.getItem('jwt')) {
        config.headers.TOKEN = sessionStorage.getItem('jwt');
    }
    return config;
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '请求发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with request error
    return Promise.reject(err);
});
//添加一个响应拦截器
ajax.interceptors.response.use(function(res) {
    //在这里对返回的数据进行处理
    if (!res.data.status) {
        if (res.data.errorCode == 401) {
            location.href = '/login'
        }
        ElementUI.Message.error({
            message: res.data.message,
            type: 'error'
        });
        throw new Error(res.data.message);
    }

    return (res.data.data ? res.data.data : res.data.status);
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '响应发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with response error
    return Promise.reject(err);
})


//管理员自身
export const adminLogin = params => {
    return ajax.post('controlPanel/login/usernameAndPassword', params);
};
export const updatePwd = params => {
    return ajax.put('admin/' + params.userId, params);
};

//管理员操作
export const getAdminLists = params => {
    return ajax.get('admin', params);
}
export const addAdmin = params => {
    return ajax.post('admin', params);
}
export const deleteAdminById = adminId => {
    return ajax.delete('admin/' + adminId);
}
export const getAdminInfoById = adminId => {
    return ajax.get('admin/' + adminId);
}
export const updateAdminById = (adminId, params) => {
    return ajax.put('admin/' + adminId, params);
}

//Banner管理
export const getBannerLists = params => {
    return ajax.get('banner', params);
}
export const addBanner = params => {
    return ajax.post('banner', params);
}
export const deleteBannerById = banner => {
    return ajax.delete('banner/' + banner);
}
export const getBannerById = bannerId => {
    return ajax.get('banner/' + bannerId);
}
export const updateBannerById = (bannerId, params) => {
    return ajax.put('banner/' + bannerId, params);
}

//视频管理
export const getVideoLists = params => {
    return ajax.get('video', params);
}
export const addVideo = params => {
    return ajax.post('video', params);
}
export const deleteVideoById = videoId => {
    return ajax.delete('video/' + videoId);
}
export const getVideoInfoById = videoId => {
    return ajax.get('video/' + videoId);
}
export const updateVideoById = (videoId, params) => {
    return ajax.put('video/' + videoId, params);
}

//视频分类管理
export const getVideoCategoryLists = params => {
    return ajax.get('videoCategory', params);
}
export const updateVideoCategoryById = (id, params) => {
    return ajax.put('videoCategory/' + id, params);
}

//合作企业管理
export const getCooperationLists = params => {
    return ajax.get('cooperativeEnterprise', params);
}
export const addCooperation = params => {
    return ajax.post('cooperativeEnterprise', params);
}
export const deleteCooperationById = id => {
    return ajax.delete('cooperativeEnterprise/' + id);
}
export const getCooperationById = id => {
    return ajax.get('cooperativeEnterprise/' + id);
}
export const updateCooperationById = (id, params) => {
    return ajax.put('cooperativeEnterprise/' + id, params);
}

//联系我们
export const getContactLists = params => {
    return ajax.get('contactUs', params);
}
export const deleteContactById = id => {
    return ajax.delete('contactUs/' + id);
}

//文件上传 前台文件需要设置一个path属性
export const uploadFiles = params => {
    return ajax.post('upload/single' + params.path, params);
};

export const getConfig = keys => {
    return ajax.get('config?keys=' + keys)
}
export const setConfig = params => {
    return ajax.post('config', params)
}